/// 定长容器 超出长度的元素会被淘汰
/// 并且初始化时最少的长度为`1`
/// ```rust
///     #[test]
///     fn test() {
///         use jormungand::basic::structure::fixed::FixedBag;
///
///         let mut bag = FixedBag::new(10);
///         for i in 0..10 {
///             bag.push(i);
///         }
///         println!("{:?}", bag);
///         //[0,1,2,3,4,5,6,7,8,9]
///         bag.push(10);
///         println!("{:?}", bag);
///         //[1,2,3,4,5,6,7,8,9,10]
///     }
/// ```
#[derive(Debug)]
pub struct FixedBag<T> {
    data: Vec<T>,
    max_len: usize,
}

impl<T> FixedBag<T> {
    pub fn new(max_len: usize) -> Self {
        if max_len == 0 { panic!("FixedBag max length at least 1.") }
        Self {
            data: Vec::<T>::with_capacity(max_len),
            max_len,
        }
    }
    pub fn push(&mut self, t: T) {
        let ultimate = self.max_len - 1;
        while self.data.len() > ultimate { self.data.remove(0); }
        self.data.push(t);
    }
    pub fn catch_data(&mut self) -> &Vec<T> {
        &self.data
    }
}

#[cfg(test)]
mod tests {
    use crate::basic::structure::fixed::FixedBag;

    #[test]
    fn test() {
        let mut vec = Vec::with_capacity(10);
        for i in 0..10 {
            vec.push(i);
        }
        println!("{:?}", vec);
        vec.push(111);
        println!("{:?}", vec);

        let mut bag = FixedBag::new(10);
        for i in 0..10 {
            bag.push(i);
        }
        println!("{:?}", bag);
        bag.push(111);
        println!("{:?}", bag);
    }
}


